/**
 * @author ahammer
 * @date 2009
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package main;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;


public class TankListCellRenderer extends JLabel implements ListCellRenderer {

	//private static DefaultListModel model;
	//Icon TankIcon;
	public TankListCellRenderer() {
		this.setOpaque( true );
		this.setVerticalTextPosition(JLabel.BOTTOM);
		this.setHorizontalTextPosition(JLabel.CENTER);
		//this.setSize(new Dimension(100, 120));
		//this.setPreferredSize(new Dimension (60,90));
	}
	
	public Component getListCellRendererComponent(JList list,
												  Object value,
												  int index,
												  boolean isSelected,
												  boolean cellHasFocus) {
		
		if (value != null){
			PlanarImage img  = null;
			boolean virtFlag = false;
			if (((DefaultListModel) value).firstElement() instanceof PlanarImage) virtFlag = false;
			if (((DefaultListModel) value).firstElement() instanceof File)        virtFlag = true;

			if (!virtFlag)img = (PlanarImage) ((DefaultListModel) value).firstElement();
			if (virtFlag) img = JAI.create("fileload", ((DefaultListModel) value).firstElement().toString());
		
			
			int numStack = ((DefaultListModel) value).getSize();
			int pixelSize = img.getColorModel().getPixelSize();
            int numBands  = img.getNumBands();
			ImageIcon TankIcon = IqmTools.createTankThumb(img);
            
			//String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
			String type = IqmTools.getImgTyp(img);
			             
		    if (virtFlag) type = type+"*";
			this.setIcon(TankIcon);
			//this.setFont(new Font("Arial", Font.PLAIN, 11));
			if (numStack == 1) this.setText(""+(index+1)+ "  "              +type);
			if (numStack >  1) this.setText(""+(index+1)+ "("+numStack+") " +type);
			if ( !isSelected ) {
				this.setBackground( Color.white );
				this.setForeground( Color.black );
				//this.setBorder(UIManager.getBorder("List.focusCellHighlightBorder")); 
			}
			else {
				this.setBackground( list.getSelectionBackground() );
				this.setForeground( list.getSelectionForeground() );
				//this.setBorder(UIManager.getBorder("List.focusCellHighlightBorder")); 
			}
			img = null;
			//return null;			
		}
		return this;
	}
}
